package com.beauty.dressing.module.market.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.framework.common.base.IFragmentPagerAdapter;
import com.framework.common.view.tablayout.CommonTabLayout;
import com.framework.common.view.tablayout.bean.BaseTabEntity;
import com.framework.common.view.tablayout.listener.CustomTabEntity;
import com.framework.common.view.tablayout.listener.OnTabSelectListener;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.base.BaseFragmentActivity;
import com.beauty.dressing.common.bean.SaleItemDetail;
import com.beauty.dressing.common.bean.SalesOrderResp;
import com.beauty.dressing.common.bean.ShopCarBean;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.common.view.NoScrollViewPager;
import com.beauty.dressing.module.market.fragments.GoodsCommentFragment;
import com.beauty.dressing.module.market.fragments.GoodsDetailFragment;
import com.beauty.dressing.module.market.fragments.GoodsFragment;
import com.beauty.dressing.R;
import com.beauty.dressing.module.market.presenter.SaleDetailPresenter;
import com.beauty.dressing.module.market.presenter.contract.SaleDetailContract;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/5/20
 */

public class GoodsDetailActivity extends BaseFragmentActivity implements OnTabSelectListener,SaleDetailContract.ViewControl {

    public static final int TAG_GOODS = 0;
    public static final int TAG_DETAIL= 1;
    public static final int TAG_COMMENT = 2;
    private long sales_id;
    private CommonTabLayout mTabLayout;
    public NoScrollViewPager mViewPager;
    private BaseFragment[] fragments;
    private ImageButton mBackImg;
    private ImageButton mShopCarBtn;
    private SaleDetailPresenter mPresenter = new SaleDetailPresenter(this);
    public static void startActivity(Context context, long sales_id){
        Intent intent = new Intent(context,GoodsDetailActivity.class);
        intent.putExtra(Constants.SALES_ID,sales_id);
        context.startActivity(intent);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_shops_detail);
        Intent intent = getIntent();
        sales_id = intent.getLongExtra(Constants.SALES_ID,0);
    }

    @Override
    public void findView() {
        mTabLayout = (CommonTabLayout) findViewById(R.id.tab_layout);
        mViewPager = (NoScrollViewPager) findViewById(R.id.view_pager);
        mBackImg = (ImageButton) findViewById(R.id.left_img_btn);
        mBackImg.setOnClickListener(this);
        mShopCarBtn = findViewById(R.id.shop_car_btn);
        mShopCarBtn.setOnClickListener(this);
        findViewById(R.id.add_shopcar_txt).setOnClickListener(this);
        findViewById(R.id.buy_txt).setOnClickListener(this);
        mTabLayout.setOnTabSelectListener(this);
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new BaseTabEntity("商品"));
        list.add(new BaseTabEntity("详情"));
        list.add(new BaseTabEntity("评价"));
        mTabLayout.setTabData(list);
    }

    @Override
    public void initData() {
        fragments = new BaseFragment[3];
        fragments[TAG_GOODS] = new GoodsFragment();
        fragments[TAG_DETAIL] = new GoodsDetailFragment();
        fragments[TAG_COMMENT] = new GoodsCommentFragment();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(fragments[TAG_GOODS]);
        fragmentList.add(fragments[TAG_DETAIL]);
        fragmentList.add(fragments[TAG_COMMENT]);
        setFragmentsParam();
        mViewPager.setOffscreenPageLimit(3);
        IFragmentPagerAdapter mPagerAdapter = new IFragmentPagerAdapter(getSupportFragmentManager());
        mPagerAdapter.setList(fragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(fragmentList.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != mTabLayout.getCurrentTab()) {
                    mTabLayout.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        ShopCarBean dbSave = controller.getDaoManager().getDaoSession().getShopCarDao().queryShopCarById(sales_id);
        if (dbSave != null){
            mShopCarBtn.setImageResource(R.drawable.detail_shopcar_green);
        } else {
            mShopCarBtn.setImageResource(R.drawable.detail_shop_car);
        }
    }

    private void setFragmentsParam() {
        for (int i = 0; i < fragments.length; i++) {
            Bundle argment = new Bundle();
            argment.putLong(Constants.SALES_ID, sales_id);
            fragments[i].setArguments(argment);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_img_btn:
                finish();
                break;
            case R.id.shop_car_btn:
            case R.id.add_shopcar_txt:
                boolean hasSale = ((GoodsFragment)fragments[TAG_GOODS]).hasSaleDetail();
                if (hasSale){
                    showProgressDialog();
                    mPresenter.salesAddCart(sales_id,1);
                }
//                if (bean != null){
//                    showToast("添加购物车成功!");
//                    mShopCarBtn.setImageResource(R.drawable.detail_shopcar_green);
//                    controller.getDaoManager().getDaoSession().getShopCarDao().insertShopCar(bean);
//                }
                break;
            case R.id.buy_txt:
                boolean hasSales = ((GoodsFragment)fragments[TAG_GOODS]).hasSaleDetail();
                if (hasSales){
                    showProgressDialog();
                    mPresenter.createSalesOrder(sales_id);
                }
                break;
        }

    }

    @Override
    public void onTabSelect(int position) {
        if (position != mViewPager.getCurrentItem()) {
            mViewPager.setCurrentItem(position, true);
        }
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onDestroy() {
        mPresenter.clear();
        fragments[TAG_GOODS].onDestroyView();
        fragments[TAG_DETAIL].onDestroyView();
        fragments[TAG_COMMENT].onDestroyView();
        super.onDestroy();

    }

    @Override
    public void onSaleDetail(boolean isSuccess, int code, String message, SaleItemDetail data) {

    }

    @Override
    public void onSaleAddCart(boolean isSuccess, int code, String message, Object data) {
        dismissProgressDialog();
        if (isSuccess){
            showToast("添加购物车成功!");
            mShopCarBtn.setImageResource(R.drawable.detail_shopcar_green);
//            controller.getDaoManager().getDaoSession().getShopCarDao().insertShopCar(bean);
        } else {
            showToast("加入购物车失败!");
        }
    }

    @Override
    public void onCreateSaleOrder(boolean isSuccess, int code, String message, SalesOrderResp data) {
        dismissProgressDialog();
        if (isSuccess && data!= null){
            WriteOrderActivity.startActivity(context,data.goods,data.defaAddress != null && !data.defaAddress.isEmpty()?data.defaAddress.get(0):null);
        } else {
            showToast("下单失败");
        }
    }
}
